package com.ikkong.goods.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.goods.model.MallGoods;
import com.ikkong.goods.service.MallGoodsService;
import com.ikkong.goods.service.impl.MallGoodsServiceImpl;
import com.ikkong.system.util.CommonUtil;

/**
 * Generated by Blade.
 * 2020-02-14 21:01:18
 */
public class MallGoodsController extends UrlPermissController {
	private static String CODE = "mallGoods";
	private static String PERFIX = "mall_goods";
	private static String LIST_SOURCE = "MallGoods.list";
	private static String BASE_PATH = "/platform/mallGoods/";
	
	MallGoodsService service = new MallGoodsServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "mallGoods.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "mallGoods_add.html");
	}

	public void edit() {
		String id = getPara(0);
		MallGoods mallGoods = service.findById(id);
//        String banner_top = mallGoods.getBanner_top();
//        String banner_middle = mallGoods.getBanner_middle();
//        String pics_middle = mallGoods.getPics_middle();
//        String cover = mallGoods.getCover();
//        List banner_middlelist = CommonUtil.stringToList(banner_middle);
//        List banner_toplist = CommonUtil.stringToList(banner_top);
//        List pics_middlelist = CommonUtil.stringToList(pics_middle);
//        setAttr("cover",cover);
//        setAttr("banner_middle",banner_middlelist);
//        setAttr("banner_top",banner_toplist);
//        setAttr("pics_middle",pics_middlelist);
        setAttr("model", JsonKit.toJson(mallGoods));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "mallGoods_edit.html");
	}

	public void view() {
		String id = getPara(0);
		MallGoods mallGoods = service.findById(id);
		setAttr("model", JsonKit.toJson(mallGoods));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "mallGoods_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		MallGoods mallGoods = mapping(PERFIX, MallGoods.class);
		mallGoods.setU_id(Integer.parseInt(ShiroKit.getUser()!=null?ShiroKit.getUser().getId().toString():"2"));
		mallGoods.setAddtime(new Date());
		boolean temp = service.save(mallGoods);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		MallGoods mallGoods = mapping(PERFIX, MallGoods.class);
		boolean temp = service.update(mallGoods);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
